// my/pages/xtxRefund/xtxRefund.js
var http = require('../../../utils/https');
Page({
  data: {
    orderInfo: null,
    refundAmount: '',
    refundDescription: '',
    uploadedImages: [],
    allOrderIemDetails: [],
  },

  onLoad(options) {
    // 接收传递过来的订单ID
    if (options.orderId) {
      this.setData({
        orderId: options.orderId
      });
      console.log('接收到的订单ID:', options.orderId);
      // 加载订单详情
      this.loadOrderDetails(options.orderId);
    }
  },

   // 加载订单详情
   loadOrderDetails(orderId) {
    console.log('加载订单详情:', orderId);

    // GET 请求
    http.GET(`/order/item/list`, {
      orderId: orderId
    }, (res) => {
      console.log('加载订单详情成功:', res);
      // 格式化价格为两位小数
      const formattedList = res.rows.map(item => ({
        ...item,
        // 四舍五入保留两位小数
        subtotal: Number(item.subtotal).toFixed(2),
        unitPrice: Number(item.unitPrice).toFixed(2)
      }));
      this.setData({
        allOrderIemDetails: formattedList
      })
      console.log('allOrderIemDetails', this.data.allOrderIemDetails);
    }, (err) => {
      console.error('加载订单详情失败:', err);
    });
    // 模拟加载订单数据
    this.setData({
      orderInfo: mock,
      // 页面展示默认金额；真正支付前会用接口覆盖 payAmount
      payAmount: this.computePayAmount(mock.totalPrice, mock.deliveryFee, mock.couponDiscount)
    });
  },

  // 返回上一页
  navigateBack() {
    wx.navigateBack();
  },

  // 退款金额输入
  onRefundAmountInput(e) {
    this.setData({
      refundAmount: e.detail.value
    });
  },

  // 退换货说明输入
  onRefundDescriptionInput(e) {
    this.setData({
      refundDescription: e.detail.value
    });
  },

  // 选择图片
  chooseImage() {
    const that = this;
    wx.chooseImage({
      count: 9 - that.data.uploadedImages.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        const uploadedImages = that.data.uploadedImages.concat(tempFilePaths);
        that.setData({
          uploadedImages: uploadedImages
        });
      }
    });
  },

  // 删除图片
  deleteImage(e) {
    const index = e.currentTarget.dataset.index;
    const uploadedImages = this.data.uploadedImages;
    uploadedImages.splice(index, 1);
    this.setData({
      uploadedImages: uploadedImages
    });
  },

  // 提交退款申请
  submitRefund() {
    const { orderId, refundAmount, refundDescription, uploadedImages } = this.data;
    
    if (!refundAmount.trim()) {
      wx.showToast({
        title: '请输入退款金额',
        icon: 'none'
      });
      return;
    }

    if (!refundDescription.trim()) {
      wx.showToast({
        title: '请填写退换货说明',
        icon: 'none'
      });
      return;
    }

    // 这里可以调用提交退款申请的接口
    console.log('提交退款申请:', {
      orderId,
      refundAmount,
      refundDescription,
      uploadedImages
    });

    wx.showToast({
      title: '申请提交成功',
      icon: 'success',
      duration: 2000,
      success: () => {
        // 延迟返回上一页
        setTimeout(() => {
          wx.navigateBack();
        }, 2000);
      }
    });
  }
});